define(["../common"], function() {
	
	var restockDoPath = g_shopDomain + "/storeRestock/restockDo/json";
	var delRestockDoPath = g_shopDomain + "/storeRestock/delDo/json";

	loadjs(["pagectrl"]);
	
	// 状态筛选
	$("[id^='srStatus_']").click(function() {
		var status = ($(this).attr("id").split("_"))[1];
		location.href = g_shopDomain + "/storeRestock/list?isChecked=" + status;
	});
	
	// 事件：全选
	$("[name='selectAll']").click(function() {
		$("[name='restockIds']").prop("checked", $(this).prop("checked"));
	});
	
	// 事件：查看订货单详情
	$("[id^='srA_']").click(function() {
		var $target = $(this).closest("tr");
		$target.toggleClass("selected");
		
		var srId = ($(this).attr("id").split("_"))[1];
		
		$detail = $target.next("[id^='detail_']");
		if ($detail.length > 0) {
			$detail.toggle();
		} else {
			// 若没有加载进货商品则异步请求加载
			storeRestockDetail(srId, 1);
		}
		
	});
	
	// 事件：翻页
	$(document).on('click', "[id^='page_']", function() {
		var vars = $(this).attr("id").split("_");
		var id = vars[1];
		var nowPage = vars[2];
		storeRestockDetail(id, nowPage);
	});
	
	// 进货商品查找
	function storeRestockDetail(restockId, nowPage) {
		
		var $target = $("#srA_" + restockId).closest("tr");
		$target.next("tr").show();
		$.post(g_shopDomain + "/storeRestockGoods/storeRestockList/part_detail", 
			{ 
				restockId: restockId,
				nowPage: nowPage,
				pageSize:5
			},
			function(data) {
				$target.next("tr").replaceWith(data);
				// 使暴露在窗体的图片显示
				$(window).scroll();
		},'html');
	}
	
	// 事件：进货单商品入库
	$("[id^='restockBt_']").click(function() {
		var datas = $(this).attr("id").split("_");
		var restockId = datas[1];
		var code = datas[2];
		var msgboxId = "restockMsgbox_" + restockId;
		var content = "确定要对 " + code + " 进货单<br/>进行入库操作吗？";
		restockDo($(this), restockId, msgboxId, content, restockDoPath, "入库成功!");
	});
	
	// 事件：删除进货单
	$("[id^='delRestockBt_']").click(function() {
		var datas = $(this).attr("id").split("_");
		var restockId = datas[1];
		var code = datas[2];
		var msgboxId = "delMsgbox_" + restockId;
		var content = "确定要删除<br/>进货单 " + code + " 吗？";
		restockDo($(this), restockId, msgboxId, content, delRestockDoPath, "删除成功!");
	});
	
	// 进货单入库或删除
	function restockDo(el, restockId, msgboxId, content, doPath, successMsg) {
		var msgConfig = {
			id: msgboxId,
			type: 'confirm',
			content: content,
			title: "提示",
			onClose: function() {
				if (this.val()) {
					$.post(doPath, {restockId: restockId}, function(data) {
						if (data.status != 0) {
							el.showMsg(data.message, 'error');
							return;
						}
						el.showMsg(successMsg, 'pass');
						// 页面删除该进货单
						el.closest("tr").remove();
						$("#detail_" + restockId).remove();
					});
				}
			}
		};
		loadjs(["msgbox"], function() {
			$.msgbox(msgConfig);
		});
	}
});